package edu.gdit.health.service.impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import edu.gdit.health.base.result.PageTableRequest;
import edu.gdit.health.base.result.Results;
import edu.gdit.health.dao.SysDictDao;
import edu.gdit.health.dao.SysReportDao;
import edu.gdit.health.model.SysDict;
import edu.gdit.health.model.SysReport;
import edu.gdit.health.service.SysReportService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.*;

//声明一个service对象，当项目启动时，Spring容器会创建一个名字叫sysReportServiceImpl的对象
@Service
public class SysReportServiceImpl implements SysReportService {

    //注入dao层对象sysReportDao,该对象由Spring容器创建并管理
    @Autowired
    SysReportDao sysReportDao;
    @Autowired
    SysDictDao sysDictDao;


    //显示Dict
    @Override
    public Map<String, List<SysDict>> getDictSet() {
        Map<String,List<SysDict>> dict=new HashMap<>();
        List<SysDict> temperature= (List<SysDict>) sysDictDao.getDict("temperature");
        List<SysDict> travel= (List<SysDict>) sysDictDao.getDict("travel");
        List<SysDict> physical_condition= (List<SysDict>) sysDictDao.getDict("physical_condition");
        //放置三对（key,value）
        dict.put("temperature",new ArrayList<SysDict>());
        dict.put("travel",new ArrayList<SysDict>());
        dict.put("physical_condition",new ArrayList<SysDict>());
        //为每对（key,value）的value赋值,第一个temperature
        for (SysDict sysDict : temperature){
            dict.get("temperature").add(new SysDict(sysDict.getDictKey(),sysDict.getDictValue()));
        }
        for (SysDict sysDict : travel){
            dict.get("travel").add(new SysDict(sysDict.getDictKey(),sysDict.getDictValue()));
        }
        for (SysDict sysDict : physical_condition){
            dict.get("physical_condition").add(new SysDict(sysDict.getDictKey(),sysDict.getDictValue()));
        }
        return dict;
    }
    public Results addReport(SysReport sysReport) {
        return Results.success(sysReportDao.save(sysReport));
    }
    //显示report
    @Override
    public Results getReports(){
        return Results.success(1000,sysReportDao.getAll());
    }
    //分页report表
    @Override
    public Results getAll(PageTableRequest pageTableRequest){
        PageHelper.startPage(pageTableRequest.getPage(),pageTableRequest.getLimit());
        List<SysReport> sysReportList=sysReportDao.getAll();
        PageInfo pageInfo = new PageInfo(sysReportList);
        return Results.success((int)pageInfo.getTotal(),sysReportList);
    }
    //数据填报页增加填报记录入sys_report
    @Override
    public Results addSysReport(SysReport sysReport) { return Results.success(sysReportDao.save(sysReport)); }
    //删除效果
    @Override
    public Results deleteReport(int id){ return Results.success(sysReportDao.delete(id));}
    //修改
    @Override
    public int updateReport(SysReport sysReport){ return sysReportDao.update(sysReport);}
    @Override
    public Results getById(int id){ return Results.success(sysReportDao.getById(id));}
    @Override
    public SysReport dictById(SysReport sysReport) {
        return sysReportDao.dictById(sysReport.getId());
    }
//    @Override
//    public Results getAllReports(int offset,int limit){
//        List<SysReport> sysAllReports=sysReportDao.getAllReports(offset,limit);
//        return Results.success(offset,limit);
//    }
}